package com.kexilo.config.domain;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 配置数据抽象模型
 * 不依赖任何具体的配置实体，完全解耦
 * 
 * @author Kexilo
 */
public class ConfigData implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 参数主键
     */
    private Long configId;
    
    /**
     * 参数名称
     */
    private String configName;
    
    /**
     * 参数键名
     */
    private String configKey;
    
    /**
     * 参数键值
     */
    private String configValue;
    
    /**
     * 系统内置（Y是 N否）
     */
    private String configType;
    
    /**
     * 备注
     */
    private String remark;
    
    /**
     * 创建者
     */
    private String createBy;
    
    /**
     * 创建时间
     */
    private LocalDateTime createTime;
    
    /**
     * 更新者
     */
    private String updateBy;
    
    /**
     * 更新时间
     */
    private LocalDateTime updateTime;
    
    // 构造器
    public ConfigData() {}
    
    public ConfigData(String configKey, String configValue) {
        this.configKey = configKey;
        this.configValue = configValue;
    }
    
    public ConfigData(String configName, String configKey, String configValue) {
        this.configName = configName;
        this.configKey = configKey;
        this.configValue = configValue;
    }
    
    // Builder模式构造器
    public static Builder builder() {
        return new Builder();
    }
    
    public static class Builder {
        private ConfigData configData = new ConfigData();
        
        public Builder configId(Long configId) {
            configData.configId = configId;
            return this;
        }
        
        public Builder configName(String configName) {
            configData.configName = configName;
            return this;
        }
        
        public Builder configKey(String configKey) {
            configData.configKey = configKey;
            return this;
        }
        
        public Builder configValue(String configValue) {
            configData.configValue = configValue;
            return this;
        }
        
        public Builder configType(String configType) {
            configData.configType = configType;
            return this;
        }
        
        public Builder remark(String remark) {
            configData.remark = remark;
            return this;
        }
        
        public Builder createBy(String createBy) {
            configData.createBy = createBy;
            return this;
        }
        
        public Builder createTime(LocalDateTime createTime) {
            configData.createTime = createTime;
            return this;
        }
        
        public Builder updateBy(String updateBy) {
            configData.updateBy = updateBy;
            return this;
        }
        
        public Builder updateTime(LocalDateTime updateTime) {
            configData.updateTime = updateTime;
            return this;
        }
        
        public ConfigData build() {
            return configData;
        }
    }
    
    // Getters and Setters
    public Long getConfigId() { return configId; }
    public void setConfigId(Long configId) { this.configId = configId; }
    
    public String getConfigName() { return configName; }
    public void setConfigName(String configName) { this.configName = configName; }
    
    public String getConfigKey() { return configKey; }
    public void setConfigKey(String configKey) { this.configKey = configKey; }
    
    public String getConfigValue() { return configValue; }
    public void setConfigValue(String configValue) { this.configValue = configValue; }
    
    public String getConfigType() { return configType; }
    public void setConfigType(String configType) { this.configType = configType; }
    
    public String getRemark() { return remark; }
    public void setRemark(String remark) { this.remark = remark; }
    
    public String getCreateBy() { return createBy; }
    public void setCreateBy(String createBy) { this.createBy = createBy; }
    
    public LocalDateTime getCreateTime() { return createTime; }
    public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; }
    
    public String getUpdateBy() { return updateBy; }
    public void setUpdateBy(String updateBy) { this.updateBy = updateBy; }
    
    public LocalDateTime getUpdateTime() { return updateTime; }
    public void setUpdateTime(LocalDateTime updateTime) { this.updateTime = updateTime; }
    
    /**
     * 是否系统内置配置
     */
    public boolean isSystemConfig() {
        return "Y".equals(this.configType);
    }
    
    /**
     * 获取布尔值
     */
    public boolean getBooleanValue() {
        if (configValue == null) {
            return false;
        }
        return "true".equalsIgnoreCase(configValue) || 
               "yes".equalsIgnoreCase(configValue) || 
               "1".equals(configValue) ||
               "Y".equalsIgnoreCase(configValue);
    }
    
    /**
     * 获取整数值
     */
    public Integer getIntegerValue() {
        try {
            return configValue != null ? Integer.parseInt(configValue) : null;
        } catch (NumberFormatException e) {
            return null;
        }
    }
    
    /**
     * 获取长整数值
     */
    public Long getLongValue() {
        try {
            return configValue != null ? Long.parseLong(configValue) : null;
        } catch (NumberFormatException e) {
            return null;
        }
    }
    
    /**
     * 获取双精度值
     */
    public Double getDoubleValue() {
        try {
            return configValue != null ? Double.parseDouble(configValue) : null;
        } catch (NumberFormatException e) {
            return null;
        }
    }
    
    /**
     * 获取默认值
     */
    public String getValueOrDefault(String defaultValue) {
        return configValue != null && !configValue.trim().isEmpty() ? configValue : defaultValue;
    }
    
    /**
     * 获取布尔值（带默认值）
     */
    public boolean getBooleanValueOrDefault(boolean defaultValue) {
        if (configValue == null || configValue.trim().isEmpty()) {
            return defaultValue;
        }
        return getBooleanValue();
    }
    
    /**
     * 获取整数值（带默认值）
     */
    public Integer getIntegerValueOrDefault(Integer defaultValue) {
        Integer value = getIntegerValue();
        return value != null ? value : defaultValue;
    }
    
    @Override
    public String toString() {
        return "ConfigData{" +
                "configId=" + configId +
                ", configName='" + configName + '\'' +
                ", configKey='" + configKey + '\'' +
                ", configValue='" + configValue + '\'' +
                ", configType='" + configType + '\'' +
                '}';
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        ConfigData that = (ConfigData) obj;
        return configId != null ? configId.equals(that.configId) : 
               (configKey != null && configKey.equals(that.configKey));
    }
    
    @Override
    public int hashCode() {
        return configId != null ? configId.hashCode() : 
               (configKey != null ? configKey.hashCode() : super.hashCode());
    }
}
